/*
 * DamagedShipEvent.java
 * Team qq 2011
 */
package com.google.code.timetrail.backend;

import java.util.Random;

/**
 * The damaged ship random event
 * 
 * @author Utkarsh
 * @version 1.1
 * 
 */
public class DamagedShipEvent extends Event {
	/**
	 * the version ID of DamagedShipEvent, CHANGE IT if control gets incompatibly changed
	 **/
	private static final long serialVersionUID = 5010L;

	/** The item that is random for the ship */
	private Item randomShipItem;

	/** The limit for the count	 */
	private static final int COUNTLIMIT = 10;
	
	/** The limit for engineer to affect probability of losing item */
	private static final double ENGINEERLIMIT = 0.95;

	/** The limit for non-engineer to affect probability of losing item */
	private static final double NONENGINEERLIMIT = 0.7;
	
	/**
	 * Damages the ship based on random chance. The chance is also depended upon
	 * whether or not the main player is an engineer.
	 * 
	 * @param gameControl
	 *            the instance of the game, to be acted upon
	 */
	public DamagedShipEvent(Control gameControl) {

		final Random rand = new Random();
		randomShipItem = gameControl.getInv().getRandomShipItem();
		int count = 0;
		while (randomShipItem.getCurrentAmount() <= 0 && count <= COUNTLIMIT) {
			randomShipItem = gameControl.getInv().getRandomShipItem();
			count++;
		}
		if (randomShipItem.getCurrentAmount() <= 0) {
			super.messageText = getDummyText();

		} else {
			if (gameControl.getPlayer() instanceof Engineer
					&& rand.nextDouble() > ENGINEERLIMIT) {
				super.formatMessage("You lost " + randomShipItem.toString());
			} else if (!(gameControl.getPlayer() instanceof Engineer)
					&& rand.nextDouble() > NONENGINEERLIMIT) {
				super.formatMessage("You lost " + randomShipItem.toString());
			} else {
				super.messageText = getDummyText();
			}
		}

	}

	/**
	 * The action for this event
	 */
	@Override
	public void action() {
		final Random rand = new Random();
		if (randomShipItem.getCurrentAmount() == 1) {
			randomShipItem.removeCurrentAmount(1);
			super.formatMessage("You lost 1 " + randomShipItem.toString());
		} else {
			final int randAmountRemoved = rand.nextInt(randomShipItem
					.getCurrentAmount() - 1) + 1;
			randomShipItem.removeCurrentAmount(randAmountRemoved);
			super.formatMessage("You lost  " + randAmountRemoved + " "
					+ randomShipItem.toString());
		}
	}

	/**
	 * The toString for this class
	 * @return the string as the class name
	 */
	@Override
	public String toString() {
		return "DamagedShipEvent";
	}
}
